package com.yugu.park.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yugu.park.business.entity.*;
import com.yugu.park.business.mapper.AdmissionRecordMapper;
import com.yugu.park.business.mapper.CardInfoMapper;
import com.yugu.park.business.mapper.MemberInfoMapper;
import com.yugu.park.business.mapper.RechargeRecordMapper;
import com.yugu.park.business.service.HomeService;
import javafx.scene.input.DataFormat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class HomeServiceImple implements HomeService {

    @Resource
    private CardInfoMapper cardInfoMapper;
    @Resource
    private AdmissionRecordMapper admissionRecordMapper;
    @Resource
    private RechargeRecordMapper rechargeRecordMapper;
    @Resource
    private MemberInfoMapper memberInfoMapper;
    @Override
    public Map<String, Integer> overview() {
        Map<String,Integer> result = new HashMap<>();
        LambdaQueryWrapper<CardInfo> wrapper = new LambdaQueryWrapper<>();
        Integer cardNum = cardInfoMapper.selectCount(wrapper);
        wrapper.apply("DATE_FORMAT(CREATE_TIME,'%Y-%m-%d') = DATE_FORMAT({0},'%Y-%m-%d')",new Date());
        Integer cardNumCur = cardInfoMapper.selectCount(wrapper);
        LambdaQueryWrapper<MemberInfo> memberInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer memberNum = memberInfoMapper.selectCount(memberInfoLambdaQueryWrapper);
        memberInfoLambdaQueryWrapper.apply("DATE_FORMAT(CREATE_TIME,'%Y-%m-%d') = DATE_FORMAT({0},'%Y-%m-%d')",new Date());
        Integer memberNumCur = memberInfoMapper.selectCount(memberInfoLambdaQueryWrapper);
        LambdaQueryWrapper<AdmissionRecord> admissionRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer admissionRecordNum = admissionRecordMapper.selectCount(admissionRecordLambdaQueryWrapper);
        admissionRecordLambdaQueryWrapper.apply("DATE_FORMAT(CREATE_TIME,'%Y-%m-%d') = DATE_FORMAT({0},'%Y-%m-%d')",new Date());
        Integer admissionRecordNumCur = admissionRecordMapper.selectCount(admissionRecordLambdaQueryWrapper);
        LambdaQueryWrapper<RechargeRecord> rechargeRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer rechargeRecordNum = rechargeRecordMapper.selectCount(rechargeRecordLambdaQueryWrapper);
        rechargeRecordLambdaQueryWrapper.apply("DATE_FORMAT(CREATE_TIME,'%Y-%m-%d') = DATE_FORMAT({0},'%Y-%m-%d')",new Date());
        Integer rechargeRecordNumCur = rechargeRecordMapper.selectCount(rechargeRecordLambdaQueryWrapper);
        result.put("cardNum",cardNum);
        result.put("memberNum",memberNum);
        result.put("admissionRecordNum",admissionRecordNum);
        result.put("rechargeRecordNum",rechargeRecordNum);
        result.put("cardNumCur",cardNumCur);
        result.put("memberNumCur",memberNumCur);
        result.put("admissionRecordNumCur",admissionRecordNumCur);
        result.put("rechargeRecordNumCur",rechargeRecordNumCur);
        return result;
    }

    @Override
    public List<LineChart> lineChartOfAdmission(List<Integer> year,String type) {
        List<LineChart> list = new ArrayList<>();
        for (Integer integer : year) {
            LineChart lineChart = new LineChart();
            lineChart.setName(String.valueOf(integer));
            lineChart.setStack("Total");
            lineChart.setType(type);
            List<Map<String, Integer>> maps = admissionRecordMapper.lineChart(integer);
            Map<String, Integer> data = new HashMap<>(16);
            for (Map<String, Integer> map : maps) {
                if (!map.isEmpty()) {
                    data.put(String.valueOf(map.get("month")), map.get("count"));
                }
            }
            lineChart.setData(data);
            list.add(lineChart);
        }
    return list;
    }

    @Override
    public List<LineChart> lineChartOfCard(List<Integer> year, String type) {
        List<LineChart> list = new ArrayList<>();
        for (Integer integer : year) {
            LineChart lineChart = new LineChart();
            lineChart.setName(String.valueOf(integer));
            lineChart.setStack("Total");
            lineChart.setType(type);
            List<Map<String, Integer>> maps = cardInfoMapper.lineChart(integer);
            Map<String, Integer> data = new HashMap<>(16);
            for (Map<String, Integer> map : maps) {
                if (!map.isEmpty()) {
                    data.put(String.valueOf(map.get("month")), map.get("count"));
                }
            }
            lineChart.setData(data);
            list.add(lineChart);
        }
        return list;
    }

    @Override
    public Map<String, Integer> genderRatio() {
        List<Map<String, Integer>> gender = cardInfoMapper.getGender();
        Map<String, Integer> data = new HashMap<>(16);
        for (Map<String, Integer> map : gender) {
            if (!map.isEmpty()) {
                data.put(map.get("SEX") == 1 ? "男" : "女",map.get("value"));
            }
        }
        return data;
    }

    @Override
    public Map<String, Integer> ageStatistics() {
        List<Map<String, Integer>> maps = cardInfoMapper.ageStatistics();
        Map<String, Integer> data = new HashMap<>(16);
        for (Map<String, Integer> map : maps) {
            if (!map.isEmpty()) {
                data.put(String.valueOf(map.get("age")), map.get("count"));
            }
        }
        return data;
    }
}
